﻿namespace ElencySolutions.ImageMap.Properties
{
    using System;
    using EPiServer.Core;
    using EPiServer.PlugIn;

    /// <summary>
    /// Multiple property base class
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "ImageMap", Description = "Image map property")]
    public class ImageMapProperty : PropertyData
    {

        #region Members

        private ImageMap _imageMap;

        #endregion Members

        #region Properties

        /// <summary>
        /// Gets the type of the property value.
        /// </summary>
        /// <value>
        /// The type of the property value.
        /// </value>
        public override Type PropertyValueType
        {
            get
            {
                return typeof(ImageMap);
            }
        }

        /// <summary>
        /// Gets the property data type.
        /// </summary>
        public override PropertyDataType Type
        {
            get
            {
                return PropertyDataType.LongString;
            }
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public override object Value
        {
            get
            {
                return _imageMap;
            }
            set
            {
                SetPropertyValue(value, delegate
                {
                    if (value == null)
                        ImageMap = new ImageMap();
                    else if (value is string)
                        ImageMap = (ImageMap)Parse((string)value).Value;
                    else if (value is ImageMap)
                        ImageMap = (ImageMap)value;
                });
            }
        }

        #endregion Properties

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ImageMapProperty"/> class.
        /// </summary>
        public ImageMapProperty()
        {
        }

        public ImageMapProperty(ImageMap imageMap)
        {
            _imageMap = imageMap;
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Creates the property control.
        /// </summary>
        /// <returns>IPropertyControl instance</returns>
        public override IPropertyControl CreatePropertyControl()
        {
            return new ImageMapPropertyControl();
        }

        /// <summary>
        /// Parses the specified value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>Current property type</returns>
        public static ImageMapProperty Parse(string value)
        {
            return new ImageMapProperty(ImageMapHelper.DeserializeObject<ImageMap>(value));
        }

        /// <summary>
        /// Parses the object string value to a PropertyData object
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>PropertyData type</returns>
        public override PropertyData ParseToObject(string value)
        {
            return Parse(value);
        }

        /// <summary>
        /// Parses a string representation to the current value type
        /// </summary>
        /// <param name="value">The value.</param>
        public override void ParseToSelf(string value)
        {
            Value = Parse(value).Value;
        }

        /// <summary>
        /// Sets the default value.
        /// </summary>
        protected override void SetDefaultValue()
        {
            ThrowIfReadOnly();
            _imageMap = new ImageMap();
        }

        /// <summary>
        /// Gets or sets the entity.
        /// </summary>
        /// <value>
        /// The entity.
        /// </value>
        public virtual ImageMap ImageMap
        {
            get
            {
                return _imageMap;
            }
            set
            {
                ThrowIfReadOnly();

                if ((!EqualTo(_imageMap, value)) || IsNull)
                {
                    _imageMap = value;
                    Modified();
                }
            }
        }

        /// <summary>
        /// Checks whether two objects are equal
        /// </summary>
        /// <param name="entity1">The entity1.</param>
        /// <param name="entity2">The entity2.</param>
        /// <returns></returns>
        private bool EqualTo(ImageMap entity1, ImageMap entity2)
        {
            if (entity1 == null || entity2 == null)
                return false;

            return string.Equals(ImageMapHelper.SerializeObject(entity1), ImageMapHelper.SerializeObject(entity2));
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        /// <param name="value">The value.</param>
        public override void LoadData(object value)
        {
            Value = value;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <param name="properties">The properties.</param>
        /// <returns></returns>
        public override object SaveData(PropertyDataCollection properties)
        {
            return ImageMapHelper.SerializeObject(Value);
        }

        #endregion Methods

    }
}
